package com.welson.restfultoolx.ui;

import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.SimpleTextAttributes;
import com.welson.restfultoolx.model.ApiInfo;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;

/**
 * API信息列表单元格渲染器
 * 自定义API列表项的显示样式
 */
public class ApiInfoListCellRenderer extends ColoredListCellRenderer<ApiInfo> {
    
    @Override
    protected void customizeCellRenderer(@NotNull JList<? extends ApiInfo> list,
                                         ApiInfo value,
                                         int index,
                                         boolean selected,
                                         boolean hasFocus) {
        if (value == null) {
            return;
        }
        
        // HTTP方法标签
        String httpMethod = value.getHttpMethod();
        Color methodColor = getHttpMethodColor(httpMethod);
        SimpleTextAttributes methodAttrs = new SimpleTextAttributes(
            SimpleTextAttributes.STYLE_BOLD, methodColor);
        append(String.format("%-6s", httpMethod), methodAttrs);
        
        // 路径
        String path = value.getPath();
        if (path != null && !path.isEmpty()) {
            append(" " + path, SimpleTextAttributes.REGULAR_ATTRIBUTES);
        }
        
        // 类名和方法名
        String className = value.getSimpleClassName();
        String methodName = value.getMethodName();
        if (className != null && methodName != null) {
            String classMethod = String.format(" (%s.%s)", className, methodName);
            append(classMethod, SimpleTextAttributes.GRAYED_ATTRIBUTES);
        }
        
        // 设置图标
        setIcon(getHttpMethodIcon(httpMethod));
    }
    
    /**
     * 根据HTTP方法获取对应的颜色
     */
    private Color getHttpMethodColor(String httpMethod) {
        if (httpMethod == null) {
            return Color.GRAY;
        }
        
        switch (httpMethod.toUpperCase()) {
            case "GET":
                return new Color(0, 128, 0);      // 绿色
            case "POST":
                return new Color(255, 165, 0);    // 橙色
            case "PUT":
                return new Color(0, 0, 255);      // 蓝色
            case "DELETE":
                return new Color(255, 0, 0);      // 红色
            case "PATCH":
                return new Color(128, 0, 128);    // 紫色
            default:
                return Color.GRAY;
        }
    }
    
    /**
     * 根据HTTP方法获取对应的图标
     */
    private Icon getHttpMethodIcon(String httpMethod) {
        // 这里可以根据需要添加不同的图标
        // 目前使用简单的圆形图标来区分不同的HTTP方法
        return createMethodIcon(getHttpMethodColor(httpMethod));
    }
    
    /**
     * 创建方法图标
     */
    private Icon createMethodIcon(Color color) {
        return new Icon() {
            @Override
            public void paintIcon(Component c, Graphics g, int x, int y) {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setColor(color);
                g2.fillOval(x + 2, y + 2, getIconWidth() - 4, getIconHeight() - 4);
                g2.setColor(color.darker());
                g2.drawOval(x + 2, y + 2, getIconWidth() - 4, getIconHeight() - 4);
                g2.dispose();
            }
            
            @Override
            public int getIconWidth() {
                return 12;
            }
            
            @Override
            public int getIconHeight() {
                return 12;
            }
        };
    }
}